package com.zjut.azhen.DBDesign.controllers.courses;

import com.zjut.azhen.DBDesign.bean.Course;
import com.zjut.azhen.DBDesign.bean.RespBean;
import com.zjut.azhen.DBDesign.bean.Role;
import com.zjut.azhen.DBDesign.bean.User;
import com.zjut.azhen.DBDesign.service.CourseService;
import com.zjut.azhen.DBDesign.service.StudentsService;
import org.apache.ibatis.annotations.Delete;
import org.apache.tomcat.util.http.parser.HttpParser;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/*
* TODO:
    4)学生所学课程及学分统计；
    7)教师任课查询；
    8)班级课程开设查询。
 * */
@RestController
@RequestMapping("/course")
public class CoursesController {

    CourseService courseService;
    StudentsService studentsService;
    public CoursesController(CourseService courseService,
                             StudentsService studentsService){
        this.courseService=courseService;
        this.studentsService=studentsService;
    }

    @GetMapping("/terms")
    public RespBean getTerms(){
        return RespBean.ok("success",courseService.getTerms());
    }




    @GetMapping("/stu/{term}")
    public RespBean getCourse(HttpSession session, @PathVariable String term){
        Role role=(Role) session.getAttribute("role");
        User user=(User) session.getAttribute("user");
        //教师无法访问
        if(user==null||"R03".equals(role.getRno())){
            return RespBean.error("无效访问");
        }
        String sno=user.getId();
        //check for term
        return RespBean.ok("success",courseService.getCourses(sno,term));
    }

        @GetMapping("/teacher/{term}")
    public RespBean getTeach(HttpSession session,@PathVariable String term){
        Role role=(Role) session.getAttribute("role");
        User user=(User) session.getAttribute("user");

        //学生无法访问
        if(user==null||"R02".equals(role.getRno())){
            return RespBean.error("无效访问");
        }
        String tno=user.getId();
        //check r term
        return RespBean.ok("success",courseService.getTeach(tno,term));
    }

    @GetMapping("/class/{term}/{classname}")
    public RespBean getClassByName(@PathVariable String term,@PathVariable String classname ,HttpSession session){

        //Role role=(Role) session.getAttribute("role");
        User user=(User) session.getAttribute("user");



//        if(user==null||"R03".equals(role.getRno())){
//            return RespBean.error("无效访问");
//        }
        //System.out.println(term);
        if("null".equals(term)){
            //System.out.println(term);
            return RespBean.error("空值错误");
        }
        List<Course> list=courseService.getByClassName(classname,term);
        if(list.size()==0){
            return RespBean.error("无课程记录或参数输入有误");
        }
        //check for term
        return RespBean.ok("success",list);
    }

    @GetMapping("/avg/{term}")
    public RespBean getAVG(HttpSession session,@PathVariable String term){
        Role role=(Role) session.getAttribute("role");
        User user=(User) session.getAttribute("user");

        //学生无法访问
        if(user==null||"R02".equals(role.getRno())){
            return RespBean.error("无效访问");
        }
        //String sno=user.getId();

        return RespBean.ok("success",courseService.getAVG(term));
    }
    //选课
    @PostMapping("/select")

    public RespBean insertSC(HttpSession session,@RequestParam(value = "cno",required = false)String cno){
        User user=(User)session.getAttribute("user");
        System.out.println(cno);
        String sno=user.getId();

        if(courseService.insertSC(sno,cno)==1) {
            return RespBean.ok("success");
        }
        return RespBean.error("insert failed");
    }
    //退课
    @DeleteMapping("/delete/{cno}")
    private RespBean deleteSC(HttpSession session, @PathVariable  String cno){
        User user=(User)session.getAttribute("user");
        String sno=user.getId();

        if(courseService.deleteSCById(sno,cno)==1){
            return RespBean.ok("success");
        }
        return RespBean.fail("delete failed");
    }

}
